/**
 * 2017年3月1日
 */
package websocket;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

/**
 * @author Alex
 *
 */
public class WebSocketCenter {
	private static ConcurrentLinkedQueue<HandshakeInterceptor> queue = new ConcurrentLinkedQueue<>();
	public static void registerListener(HandshakeInterceptor inter){
		queue.add(inter);
	}
	public static void beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, Map<String, Object> attributes){
		try{
			for(HandshakeInterceptor hi:queue){
				hi.beforeHandshake(request, response, wsHandler, attributes);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
			WebSocketHandler wsHandler, Exception exception){
		try{
			for(HandshakeInterceptor hi:queue){
				hi.afterHandshake(request, response, wsHandler, exception);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
